package com.fingersoft.im.utils

import android.Manifest
import android.content.Context
import android.telephony.TelephonyManager
import android.text.TextUtils
import java.lang.reflect.Method
import android.bluetooth.BluetoothAdapter
import android.util.DisplayMetrics
import android.view.WindowManager
import cn.fingersoft.io.rong.imlib.common.DeviceUtils

/**
 * Created by wc on 2017/12/26.
 */
class EnvironmentUtils{
    companion object {
        fun getOSVersion():String{
            return android.os.Build.VERSION.RELEASE
        }

        fun getProductName():String{
            return android.os.Build.PRODUCT
        }

        fun getModelName():String{
            return android.os.Build.MODEL
        }

        fun getOSName():String{
            return "android"
        }
        //系统定制商名称(商标)
        fun getBrand():String{
            return android.os.Build.BRAND
        }

        fun getDeviceName():String{
            var deviceName = "Unknown"
            try {
                val myDevice: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
                deviceName = myDevice?.name ?: "Unknown"
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return deviceName
        }
        //获取手机分辨率,高*宽
        fun getDisplayMetrics(context:Context):String{
            val mDisplayMetrics = DisplayMetrics()//屏幕分辨率容器
            val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            wm.getDefaultDisplay().getMetrics(mDisplayMetrics)
            val width = mDisplayMetrics.widthPixels
            val height = mDisplayMetrics.heightPixels
            return height.toString() +'x'+ width.toString()
        }
        fun getImeiAndMeid(mContext:Context):String{
            var method:Method?
            var imei1:String?
            var imei2:String?
            var meid:String?
            if (AppUtils.isUsingSolidKey() && !PermissionsUtils.isGrantedPermissions(arrayOf(Manifest.permission.READ_PHONE_STATE), mContext)) {
                return DeviceUtils.getDeviceIMEI(mContext)
            }
            var manager:TelephonyManager= mContext
                    .getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            try{
                method = TelephonyManager::class.java.getMethod("getDeviceId", Int::class.java)
                imei1 = manager.getDeviceId()
                imei2 = method.invoke(manager, 1) as String?
                meid = method.invoke(manager, 2) as String?
            }catch (e:Exception){
                e.printStackTrace()
                return DeviceUtils.getDeviceIMEI(mContext)
            }catch (e:kotlin.Exception){
                e.printStackTrace()
                return DeviceUtils.getDeviceIMEI(mContext)
            }
            if (TextUtils.isEmpty(imei1)){
                imei1="0"
            }
            if (TextUtils.isEmpty(imei2)){
                imei2="0"
            }
            if (TextUtils.isEmpty(meid)) {
                meid = DeviceUtils.getEBenUUID(mContext)
                if (null == meid || TextUtils.isEmpty(meid)) {
                    meid = "0"
                }
            }
            return "$imei1,$imei2,$meid"
        }
    }
}
